package com.boot.security.server.controller.mffl.mffl_ImageAndText;

import com.boot.security.server.service.impl.mffl.WXPay.WXPayService;
import com.boot.security.server.utils.mffl.ReturnBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.boot.security.server.service.impl.mffl.MfflImageAndText.MfflImageAndTextService;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/client/mflimageandtext")
@Api(tags = "图文咨询")
public class Cilent_MfflImageAndTextController {

    //图文咨询
    @Autowired
    MfflImageAndTextService MfflImageAndTextService;

    //微信支付
    @Autowired
    WXPayService wxPayService;

    @PostMapping(value = "imageandtextorder")
    @ApiOperation(value = "图文咨询下单")
    public ReturnBean imageAndTextOrder(@ApiParam(name = "lawyerid",value = "律师id",required = true) @RequestParam Long lawyerid){
        if(lawyerid == null){
            return new ReturnBean("201", "服务类型id,不能为空");
        }
        try {
            return MfflImageAndTextService.imageandtextOrder(lawyerid);
        }catch(Exception e){
            return new ReturnBean("201",e.getMessage());
        }
    }


    @GetMapping(value = "pay")
    @ApiOperation(value = "支付")
    public ReturnBean pay(@ApiParam(name = "paymerchantnumber", value = "支付订单号", required = true) @RequestParam String paymerchantnumber) {
        if (StringUtils.isEmpty(paymerchantnumber)) {
            return new ReturnBean("201", "paymerchantnumber不能为空");
        }
        return MfflImageAndTextService.ServicePay(paymerchantnumber, "/client/mfflContracts/wechatpaycallback");
    }


    @ApiIgnore
    @RequestMapping(value = "wechatpaycallback")
    @ResponseBody
    @Transactional
    public String WechatPayCallback(HttpServletRequest request) throws Exception {
        //处理微信回调数据
        Map<String, String> returnmap = wxPayService.WechatPayCallback(request);
        //修改订单信息
        boolean b = MfflImageAndTextService.WechatPayCallback(returnmap);
        //微信回调数据
        return wxPayService.WechatPayCallbackReturnData(b);
    }


    @PostMapping(value = "orderquery")
    @ApiOperation(value = "查询订单状态，在确认用户完成支付后调用。（不信任回调函数）")
    public ReturnBean orderquery(@ApiParam(name = "payMerchantNumber", value = "商户订单号", required = true) @RequestParam String payMerchantNumber) throws Exception {
        if(StringUtils.isEmpty(payMerchantNumber)){
            return new ReturnBean("201", "paymerchantnumber不能为空");
        }
        return MfflImageAndTextService.orderquery(payMerchantNumber);
    }
}
